﻿using Microsoft.UI;
using Microsoft.UI.Composition;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Windows.UI;

namespace WinUITest.Pages
{
    [Menu("自定义画刷")]
    public sealed partial class CustomBrushPage : Page
    {
        public CustomBrushPage() => InitializeComponent();
    }
    public sealed class CustomBrush : XamlCompositionBrushBase
    {
        public Color Color
        {
            get => (Color)GetValue(ColorProperty);
            set => SetValue(ColorProperty, value);
        }
        public static readonly DependencyProperty ColorProperty = DependencyProperty.Register(nameof(Color), typeof(Color), typeof(CustomBrush), new PropertyMetadata(Colors.Transparent));
        protected override void OnConnected()
        {
            if (CompositionBrush is not null) return;
            Window? window = App.MainWindow;
            if (window is null) return;
            Compositor compositor = window.Compositor;
            CompositionColorBrush redBrush = compositor.CreateColorBrush(Color);
            CompositionBrush = redBrush;
        }
        protected override void OnDisconnected()
        {
            CompositionBrush?.Dispose();
            CompositionBrush = null;
        }
    }
}
